package com.hcb.fragment;

import java.util.ArrayList;

import com.hcb.app911.MainActivity;
import com.hcb.app911.R;
import com.hcb.webservice.InternetUtils;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;

public class SearchForResponder extends Fragment implements OnClickListener {
	// TextView txtSearch;
	// Button btnHospital;
	// String contentSearch;
	Button btnHospital, btnPolice, btnfireservice, btnambulance;
	ArrayList<Object> arrData;
	EditText edSearch;
	int type = 0;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(
				R.layout.fragment_search_for_responder, container, false);
		((MainActivity)this.getActivity()).getImgDetail().setImageResource(R.drawable.mydetail);
		((MainActivity)this.getActivity()).getImgHome().setImageResource(R.drawable.icon);
		btnHospital = (Button) rootView.findViewById(R.id.btnHospital);
		btnPolice = (Button) rootView.findViewById(R.id.btnPolice);
		btnfireservice = (Button) rootView.findViewById(R.id.btnfireservice);
		btnambulance = (Button) rootView.findViewById(R.id.btnambulance);
		edSearch = (EditText) rootView.findViewById(R.id.edSearch);
		edSearch.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_SEARCH) {
					if (InternetUtils.getInstance().isConnectingToInternet(
							getActivity())) {
						performSearch();
					} else {
						Toast.makeText(getActivity(),
								"No network please connect network",
								Toast.LENGTH_LONG).show();
					}
					return true;
				}
				return false;
			}
		});
		btnHospital.setOnClickListener(this);
		btnPolice.setOnClickListener(this);
		btnfireservice.setOnClickListener(this);
		btnambulance.setOnClickListener(this);
		// txtSearch = (TextView) rootView.findViewById(R.id.txtSearch);
		// btnHospital = (Button) rootView.findViewById(R.id.btnHospital);
		// btnHospital.setOnClickListener(this);
		return rootView;
	}

	public void performSearch() {
		String search = edSearch.getText().toString();
		if (!search.equalsIgnoreCase("")) {
			Fragment fragment = new ListSearchResponce();
			FragmentManager fragmentManager = getActivity()
					.getSupportFragmentManager();
			FragmentTransaction ft = fragmentManager.beginTransaction();
			Bundle bundle = new Bundle();
			bundle.putString("keyseach", search);

			if (fragment != null) {
				fragment.setArguments(bundle);
				ft.remove(fragment);
				ft.replace(R.id.frame_container, fragment);
				ft.commit();
			}
		} else {

		}

	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		Fragment fragment = new ListSearchResponceFragment();
		FragmentManager fragmentManager = getActivity()
				.getSupportFragmentManager();
		FragmentTransaction ft = fragmentManager.beginTransaction();
		Bundle bundle = new Bundle();
		String search = edSearch.getText().toString();
		bundle.putString("keyseach", search);
		switch (id) {
		case R.id.btnHospital:
			if (InternetUtils.getInstance().isConnectingToInternet(
					getActivity())) {
				bundle.putString("responder_type", "1");
				type = 1;
			} else {
				Toast.makeText(getActivity(),
						"No network please connect network", Toast.LENGTH_LONG)
						.show();
			}
			break;
		case R.id.btnPolice:
			if (InternetUtils.getInstance().isConnectingToInternet(
					getActivity())) {
				bundle.putString("responder_type", "2");
				type = 2;
			} else {
				Toast.makeText(getActivity(),
						"No network please connect network", Toast.LENGTH_LONG)
						.show();
			}
			break;

		case R.id.btnfireservice:
			if (InternetUtils.getInstance().isConnectingToInternet(
					getActivity())) {
				bundle.putString("responder_type", "3");
				type = 3;
			} else {
				Toast.makeText(getActivity(),
						"No network please connect network", Toast.LENGTH_LONG)
						.show();
			}
			break;

		case R.id.btnambulance:
			if (InternetUtils.getInstance().isConnectingToInternet(
					getActivity())) {
				bundle.putString("responder_type", "4");
				type = 4;
			} else {
				Toast.makeText(getActivity(),
						"No network please connect network", Toast.LENGTH_LONG)
						.show();
			}
			break;
		default:
			break;
		}
		if (fragment != null) {
			fragment.setArguments(bundle);
			ft.remove(fragment);
			ft.replace(R.id.frame_container, fragment);
			ft.commit();
		}

	}
}
